package si.ibloc.cms.logic.impl;

import java.io.Serializable;
import java.util.Date;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.bpm.CreateProcess;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.log.Log;
import si.ibloc.cms.model.Article;
import si.ibloc.cms.model.Content;
import si.ibloc.cms.model.File;
import si.ibloc.cms.model.User;

/**
 * User: maticm<br/>
 * Date: 23.8.2007
 * <p/>
 * <p>(C) Genera Lynx, d.o.o.</p>
 */
@Name("contentCreator")
@Scope(ScopeType.CONVERSATION)
public class ContentCreator implements Serializable {
   @Logger private transient Log log;
   @In private User user;

   private boolean isFile = false;
   private Content content;

   public String createNew() {
      content = isFile ? new File() : new Article();
      content.setAuthor(user);
      content.setCreationDate(new Date());
      log.info("Created new content: #0", content);
      return content.getTypeName();
   }

   @End
   @CreateProcess(definition = "publish")
   @Restrict
   public void saveContent() {
      log.info("Saving content to business process: " + content);
      Contexts.getBusinessProcessContext().set("pubContent", content);
   }

   public Content getContent() {
      return content;
   }

   public boolean isFile() {
      return isFile;
   }

   public void setFile(boolean file) {
      isFile = file;
   }
}
